package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.freedom.yefeng.yfrecyclerview.YfMultiTypeListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemFollowInfoLayoutBinding;
import com.jinlejia.erp.databinding.ItemFollowVoiceLayoutBinding;
import com.jinlejia.erp.utils.DateUtil;
import com.jinlejia.erp.widget.musicView.MusicView;

import java.util.ArrayList;
import java.util.Map;


/**
 * 房源跟进Adapter
 * Created by Administrator on 2017/4/27 0027.
 */

public class HouseFollowAdapter extends YfMultiTypeListAdapter {
    ArrayList<Map<String, Object>> dataList;
    private Context context;
    private int cur_play = -1;

    public HouseFollowAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
        dataList = data;
    }

    public void setDataList(ArrayList<Map<String, Object>> data) {
        super.setData(data, YfListMode.MODE_DATA);
        this.dataList = data;
        notifyDataSetChanged();
    }

    public void addDataList(ArrayList<Map<String, Object>> data) {
        super.addData(data);
        this.dataList.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public void onBindCustomDataViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        super.onBindDataViewHolder(holder, position);
        if (holder instanceof InfoViewHolder) {
            ItemFollowInfoLayoutBinding binding = DataBindingUtil.getBinding(holder.itemView);
            binding.followTime.setText(DateUtil.getTime(dataList.get(position).get("followTime").toString(), "yyyy-MM-dd HH:mm"));
            if (dataList.get(position).containsKey("followUserName"))
                binding.brokerInfo.setText(dataList.get(position).get("followUserName") + "      " + dataList.get(position).get("dept"));
            if (dataList.get(position).containsKey("userName"))
                binding.brokerInfo.setText(dataList.get(position).get("userName") + "      " + dataList.get(position).get("dept"));

            binding.info.setText(dataList.get(position).get("followContent").toString());
            if (dataList.get(position).containsKey("followType"))
                binding.type.setText(dataList.get(position).get("followType").toString());
            if (dataList.get(position).containsKey("followStyle"))
                binding.type.setText(dataList.get(position).get("followStyle").toString());
        } else if (holder instanceof VoiceViewHolder) {
            final ItemFollowVoiceLayoutBinding binding = DataBindingUtil.getBinding(holder.itemView);
            //音频
            final VoiceViewHolder voiceViewHolder = (VoiceViewHolder) holder;
            voiceViewHolder.setIsRecyclable(true);
            binding.followTime.setText(DateUtil.getTime(dataList.get(position).get("followTime").toString(), "yyyy-MM-dd HH:mm"));

            if (dataList.get(position).containsKey("followUserName"))
                binding.brokerInfo.setText(dataList.get(position).get("followUserName") + "      " + dataList.get(position).get("dept"));
            if (dataList.get(position).containsKey("userName"))
                binding.brokerInfo.setText(dataList.get(position).get("userName") + "      " + dataList.get(position).get("dept"));

            binding.phoneNo.setText(dataList.get(position).get("callingId").toString());

            if (dataList.get(position).containsKey("followType"))
                binding.type.setText(dataList.get(position).get("followType").toString());
            if (dataList.get(position).containsKey("followStyle"))
                binding.type.setText(dataList.get(position).get("followStyle").toString());
            //录音保护
            if ( dataList.get(position).containsKey("followState") && dataList.get(position).get("followState").equals("4")) {
                binding.protect.setVisibility(View.VISIBLE);
                binding.musicView.setVisibility(View.GONE);
            }else{
                binding.musicView.setVisibility(View.VISIBLE);
                binding.protect.setVisibility(View.GONE);
            }
            binding.musicView.reset();
            binding.musicView.setOnControlClickListener(new MusicView.OnControlClickListener() {
                @Override
                public void onClick() {
                    if( cur_play != position ){
                        notifyItemChanged(cur_play);
                    }
                    binding.musicView.setPlayUrl(dataList.get(position).get("followContent").toString());
                    binding.musicView.controlBtnClick();
                        cur_play = position;
                }
            });
            binding.callIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != callListener) {
                        callListener.onCall(dataList.get(position).get("callingId").toString());
                    }
                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateCustomDataViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == 10) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_info_layout, parent, false);
            return new InfoViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_voice_layout, parent, false);
            return new VoiceViewHolder(view);
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }


    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {

        if (dataList.get(position).containsKey("followType") || dataList.get(position).containsKey("followStyle")) {

            //返回值大于5 ，防止和库里的type冲突
            if ((dataList.get(position).containsKey("followType") && dataList.get(position).get("followType").toString().equals("录音跟进"))
                    || (dataList.get(position).containsKey("followStyle") && dataList.get(position).get("followStyle").toString().equals("录音跟进"))) {
                return 11;
            } else {
                return 10;
            }
        } else {
            return super.getItemViewType(position);
        }
    }

    static final class InfoViewHolder extends RecyclerView.ViewHolder {


        public InfoViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    static final class VoiceViewHolder extends RecyclerView.ViewHolder {

        public VoiceViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


    public interface OnCallListener {
        void onCall(String tel);
    }

    OnCallListener callListener;

    public void setOnCallListener(OnCallListener listener) {
        this.callListener = listener;
    }


}
